#pragma once

#include <iostream>
#include <assert.h>
#include <string>
#include <list>
#include <deque>
#include <queue>
#include<vector>
#include<functional>

using namespace std;

namespace bit
{
    template <class T>
    struct less
    {
        bool operator()(const T& e1, const T& e2)
        {
            return e1 < e2;
        }
    };

    template <class T>
    struct greater
    {
        bool operator()(const T& e1, const T& e2)
        {
            return e1 > e2;
        }
    };

    template <class T, class Container = std::vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        void adjust_down(int parent)
        {
            int leftchild = parent * 2 + 1;
            while (leftchild < c.size())
            {
                if (leftchild + 1 < c.size() && comp(c[leftchild], c[leftchild + 1]))
                {
                    leftchild++;
                }

                if (comp(c[leftchild], c[parent]))
                {
                    std::swap(c[leftchild], c[parent]);
                    parent = leftchild;
                    leftchild = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[child], c[parent]))
                {
                    std::swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            InputIterator it = first;
            while (it != last)
            {
                push(*it);
                ++it;
            }
        }

        bool empty() const
        {
            return 0 == size();
        }

        size_t size() const
        {
            return c.size();
        }

        T& top() const
        {
            return c[0];
        }

        void push(const T& x)
        {
            c.push_back(x);
            adjust_up(c.size() - 1);
        }

        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            adjust_down(0);
        }
    private:
        Container c;
        Compare comp;
    };
};